
// 拿到教学任务数据后的处理函数
const render = (res) => {
    let datas = res.data;
    if (datas.length != 0){
        let tbody_inner = '';
        for (let i = 0; i < datas.length; i++) {
            tbody_inner += `
            <tr>
                <td>${datas[i].cl_name}</td>
                <td>${datas[i].te_name}</td>
                <td>${datas[i].co_name}</td>
                <td>${datas[i].co_type}</td>
                <td>${datas[i].co_weekh}</td>
                <td>${datas[i].co_week}</td>
                <td>${datas[i].co_totalh}</td>
                <td>${datas[i].co_exam_type}</td>
            </tr>
            `;
        }
        $('.table tbody').html(tbody_inner);
        $('.table+p').empty();
    }else{
        $('.table tbody').empty();
        $('.table+p').html(`无记录`)
    }
}

// 教学任务查询
function taskQuery(){
    let year,term;
    year = $('#year').val();
    term = $('#term').val();

    let el_id = this.id;

    switch (el_id){
        // 学院查询
        case 'depq':
            let dep = $('#dep').val();
            getRequest(`../inc/data/queryTask.php?task-term=${year}-${term}&ma-dep=${dep}`).then(render)
            break
        // 班级查询
        case 'classq':
            let cl_id = $('#class').val();
            getRequest(`../inc/data/queryTask.php?task-term=${year}-${term}&cl-id=${cl_id}`).then(render);
            break
        // 课程查询
        case 'courseq':
            let co_id = $('#course').val();
            getRequest(`../inc/data/queryTask.php?task-term=${year}-${term}&co-id=${co_id}`).then(render);
            break
        // 教师查询
        case 'teq':
            let te_id = $('#te').val();
            getRequest(`../inc/data/queryTask.php?task-term=${year}-${term}&te-id=${te_id}`).then(render);
            break
    }
    
}


// 绑定查询按钮事件
$('#depq').click(taskQuery);
$('#classq').click(taskQuery);
$('#courseq').click(taskQuery);
$('#teq').click(taskQuery);



// 默认查询（进入页面时默认显示的内容）
let datas = [];
getRequest(`../inc/data/queryTask.php?task-term=2021-2022-1&ma-dep=1`).then((res) => {
    datas = res.data
    let tbody_inner = ''
    for (let i=0; i<datas.length; i++){
        tbody_inner += `
        <tr>
            <td>${datas[i].cl_name}</td>
            <td>${datas[i].te_name}</td>
            <td>${datas[i].co_name}</td>
            <td>${datas[i].co_type}</td>
            <td>${datas[i].co_weekh}</td>
            <td>${datas[i].co_week}</td>
            <td>${datas[i].co_totalh}</td>
            <td>${datas[i].co_exam_type}</td>
        </tr>
        `
    }
    $('.table tbody').html(tbody_inner);
})

// 学年列表实时渲染
const yearNow = Number((new Date()).getFullYear());

function studyYear(yearNow){
    let studyYearHtml = `<option value="${yearNow}-${yearNow+1}">${yearNow}-${yearNow+1}</option>`;
    let tmp01 = yearNow - 1;
    let tmp02 = yearNow;
    while (tmp01 >= 2013){
        studyYearHtml = `<option value="${tmp01}-${tmp02}" selected>${tmp01}-${tmp02}</option>` + studyYearHtml;
        tmp01 -= 1;
        tmp02 -= 1;
    }
    return studyYearHtml
}

$('#year').html(studyYear(yearNow));


// 学院选项改变事件
function select_dep(){
    let dep_id = $('#dep').val();

    // 查询查找条件
    $.get(`../inc/data/getCondition.php?dep_id=${dep_id}`, function(data){
        // 渲染学院下的班级
        if (data['cls'].length != 0){
            
            let html = '';
            for (let idx=0; idx<data['cls'].length; idx++){
                html += `
                <option value="${data['cls'][idx].cl_id}">${data['cls'][idx].cl_name}</option>
                `;
            }
            $('#class').html(html);
        }else{
            console.log('start cls')
            $('#class').html('<option>暂无记录</option>');
        }

        // 渲染学院下的课程
        if (data['courses'].length != 0){
            let html = '';
            for (let idx=0; idx<data['courses'].length; idx++){
                html += `
                <option value="${data['courses'][idx].co_id}">${data['courses'][idx].co_name}</option>
                `;
            }
            $('#course').html(html);
        }else{
            $('#course').html('<option>暂无记录</option>');
        }

        // 渲染学院下的教师
        if (data['tes'].length != 0){
            let html = '';
            for (let idx=0; idx<data['tes'].length; idx++){
                html += `
                <option value="${data['tes'][idx].te_id}">${data['tes'][idx].te_name}</option>
                `
            }
            $('#te').html(html);
        }else{
            $('#te').html('<option>暂无记录</option>')
        }
    });
    
}
select_dep();
$('#dep').change(select_dep)


